﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using RainyPWS.Business;
using RainyPWS.Web.Admin.controls;
using RainyPWS.Common;

namespace RainyPWS.Web.Admin
{
    public partial class RecordManage : System.Web.UI.Page
    {
        private int pageSize = 10;
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "记事";
            Master.ShowTitle(controls.Title.IconType.Record);

            if (!IsPostBack)
            {
                Data_Bind(1);
            }
        }

        private void Data_Bind(int pageIndex)
        {
            int recordCount;
            int typeId = MRequest.GetInt("tid", 0);
            int status = RecordStatus.All;
            if (typeId == -1)
            {
                typeId = 0;
                status = RecordStatus.Deleted;
            }
            DataTable dt = BRecord.Instance.GetDataForPage(typeId, status, DateTime.MinValue, DateTime.MinValue, string.Empty, pageIndex, pageSize, out recordCount);
            dt = UpdateButtonState(dt);
            DataRepeater.DataSource = dt;
            DataRepeater.DataBind();
            Pager1.RecordCount = recordCount;
            Pager1.PageSize = pageSize;
            Pager1.PageIndex = pageIndex;
        }

        private DataTable UpdateButtonState(DataTable dt)
        {
            DataColumn dc = new DataColumn("edit");
            dt.Columns.Add(dc);
            dc = new DataColumn("delete");
            dt.Columns.Add(dc);
            dc = new DataColumn("undelete");
            dt.Columns.Add(dc);
            dc = new DataColumn("publish");
            dt.Columns.Add(dc);
            dc = new DataColumn("unpublish");
            dt.Columns.Add(dc);
            foreach (DataRow dr in dt.Rows)
            {
                int state = Utils.StrToInt(dr["status"], 0);
                switch (state)
                {
                    case RecordStatus.Ready:
                        dr["edit"] = "";
                        dr["delete"] = "";
                        dr["undelete"] = "style=\"display:none;\"";
                        dr["publish"] = "";
                        dr["unpublish"] = "style=\"display:none;\"";
                        break;
                    case RecordStatus.Published:
                        dr["edit"] = "";
                        dr["delete"] = "";
                        dr["undelete"] = "style=\"display:none;\"";
                        dr["publish"] = "style=\"display:none;\"";
                        dr["unpublish"] = "";
                        break;
                    case RecordStatus.Deleted:
                        dr["edit"] = "style=\"display:none;\"";
                        dr["delete"] = "style=\"display:none;\"";
                        dr["undelete"] = "";
                        dr["publish"] = "style=\"display:none;\"";
                        dr["unpublish"] = "style=\"display:none;\"";
                        break;
                    default:
                        dr["edit"] = "";
                        dr["delete"] = "";
                        dr["undelete"] = "style=\"display:none;\"";
                        dr["publish"] = "";
                        dr["unpublish"] = "style=\"display:none;\"";
                        break;
                }
            }
            return dt;
        }

        protected void Pager1_PagerIndexChanged(object sender, PagerCommandEventArgs e)
        {
            if (e.Command == PagerCommandType.First)
            {
                Data_Bind(1);
            }
            else if (e.Command == PagerCommandType.Prev)
            {
                Data_Bind(e.PageIndex - 1);
            }
            else if (e.Command == PagerCommandType.Next)
            {
                Data_Bind(e.PageIndex + 1);
            }
            else if (e.Command == PagerCommandType.Last)
            {
                Data_Bind(StaticVar.MaxPageCount);
            }
            else if (e.Command == PagerCommandType.Refresh || e.Command == PagerCommandType.Num)
            {
                Data_Bind(e.PageIndex);
            }
            else
            {
                Data_Bind(1);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            Data_Bind(Pager1.PageIndex);
        }
    }
}